package cn.rongcloud.im.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.rongcloud.im.R;
import cn.rongcloud.im.ui.adapter.MoudleAdapter;
import cn.rongcloud.im.ui.bean.MoudleChildBean;
import cn.rongcloud.im.ui.bean.MoudleGroupBean;
import cn.rongcloud.im.utils.SpacesItemDecoration;

/**
 * Created by lincheng on 2017/3/14.
 */

public class LifeFragment extends Fragment {
    /**LifeFragment的顶部RecycleListView*/
    private RecyclerView mLifeHeadRecycleListView;
    /**LifeFragment的底部RecycleListView*/
    private RecyclerView mLifeBottomRecycleListView;
    /**顶部的TextView*/
    private TextView mHeadTextView;
    /**底部的TextView*/
    private TextView mBottomTextView;
    /**存放LifeMoulde组的List*/
    private List<MoudleGroupBean> mLifeMoudleGroupList;
    /**LifeMoudle的适配器*/
    private MoudleAdapter mMoudleAdapter;
    /**每行显示的模块数量*/
    private int mColumns = 3;
    /**每个模块之间的间隔*/
    private int mSpacesItemDecoration =1;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.activity_moudle,null);
        initParams();
        initView(view);
        setView();
        setListener();
        return view;
    }

    /**
     * 设置控件监听
     */
    private void setListener() {
    }

    /**
     * 设置控件
     */
    private void setView() {
        //设置头部RecycleView
        setHeadRecycleView();
        //设置底部RecycleView
        setBottomRecycleView();
        mHeadTextView.setText(mLifeMoudleGroupList.get(0).getMoudleGroupName());
        mBottomTextView.setText(mLifeMoudleGroupList.get(1).getMoudleGroupName());
    }

    /**
     * 设置顶部RecycleView
     */
    private void setBottomRecycleView() {
        mLifeBottomRecycleListView.setLayoutManager(new GridLayoutManager(getContext(),3){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mLifeBottomRecycleListView.addItemDecoration(new SpacesItemDecoration(mSpacesItemDecoration));
        mMoudleAdapter = new MoudleAdapter(getContext() ,mLifeMoudleGroupList.get(1).getMoudleChildList());
        mLifeBottomRecycleListView.setAdapter(mMoudleAdapter);
    }

    /**
     * 设置顶部RecycleView
     */
    private void setHeadRecycleView() {
        mLifeHeadRecycleListView.setLayoutManager(new GridLayoutManager(getContext(),3){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mLifeHeadRecycleListView.addItemDecoration(new SpacesItemDecoration(mSpacesItemDecoration));
        mMoudleAdapter = new MoudleAdapter(getContext() ,mLifeMoudleGroupList.get(0).getMoudleChildList());
        mLifeHeadRecycleListView.setAdapter(mMoudleAdapter);
    }

    /**
     * 初始化参数
     */
    private void initParams() {
        mLifeMoudleGroupList = new ArrayList<>();
        makeFuseData();
        fullData();
    }

    /**
     * 填充数据，如果数据%3不为0的话，需要增加相应的空数据
     */
    private void fullData() {
        for(int i = 0 ;i <mLifeMoudleGroupList.size(); i++){
            int size = mLifeMoudleGroupList.get(i).getMoudleChildList().size();
            int count = size % mColumns;
            if(count == 0){
                return;
            }
            int lenth = 3 - count;
            MoudleChildBean childBean;
            for(int j = 0 ; j < lenth ; j++){
                childBean = new MoudleChildBean();
                childBean.setMoudleChildName("");
                childBean.setSpace(true);
                mLifeMoudleGroupList.get(i).getMoudleChildList().add(childBean);
            }
        }
    }
    /**
     * 制造假的数据
     */
    private void makeFuseData() {
        MoudleGroupBean groupBean;
        List<MoudleChildBean> moudleChildList;

        groupBean = new MoudleGroupBean();
        groupBean.setMoudleGroupName("生活服务");
        moudleChildList = new ArrayList<>();
        addChildToList(moudleChildList ,"邻里圈" ,R.drawable.icon_friend_space);
        addChildToList(moudleChildList ,"活动" ,R.drawable.icon_activity);
        addChildToList(moudleChildList ,"发现" ,R.drawable.icon_discover);
        addChildToList(moudleChildList ,"扫一扫" ,R.drawable.icon_scan);
        groupBean.setMoudleChildList(moudleChildList);
        mLifeMoudleGroupList.add(groupBean);

        groupBean = new MoudleGroupBean();
        groupBean.setMoudleGroupName("我的关注");
        moudleChildList = new ArrayList<>();
        addChildToList(moudleChildList ,"永辉微店" ,R.drawable.icon_yh);
        addChildToList(moudleChildList ,"周黑鸭" ,R.drawable.icon_zhy);
        addChildToList(moudleChildList ,"e鲜果" ,R.drawable.icon_exg);
        addChildToList(moudleChildList ,"快克便利" ,R.drawable.icon_kk);
        addChildToList(moudleChildList ,"星巴克" ,R.drawable.icon_xbk);
        addChildToList(moudleChildList ,"更多" ,R.drawable.icon_application_manager);
        groupBean.setMoudleChildList(moudleChildList);
        mLifeMoudleGroupList.add(groupBean);
    }

    /**
     * 把子的item数据加入到List
     * @param moudleChildList
     * @param mouldeName
     * @param mouldeImage
     */
    private void addChildToList(List<MoudleChildBean> moudleChildList, String mouldeName, int mouldeImage) {
        MoudleChildBean childBean;
        childBean = new MoudleChildBean();
        childBean.setMoudleChildName(mouldeName);
        childBean.setMoudleChildImage(mouldeImage);
        moudleChildList.add(childBean);
    }


    /**
     * 初始化View
     * @param view     LifeFragment的View
     */
    private void initView(View view) {
        mLifeHeadRecycleListView = (RecyclerView) view.findViewById(R.id.rv_moudle_head_list);
        mLifeBottomRecycleListView = (RecyclerView) view.findViewById(R.id.rv_moudle_bottom_list);
        mHeadTextView = (TextView) view.findViewById(R.id.tv_moulde_head_group_name);
        mBottomTextView= (TextView) view.findViewById(R.id.tv_moulde_bottom_group_name);
    }
}
